//
//  bulletObject.m
//  TowerDefence
//
//  Created by K3 on 5/30/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BulletObject.h"
#import "PlayScreen.h"
#import "TowerObject.h"

@implementation BulletObject

@synthesize velocity, damage, tower;

-(id)initBulletObjectWith:(GameLayer *)_gameLayer tower:(GameObject *)_tower {
    self = [super initGameObjectWith:_gameLayer];
    tower = _tower;
    
    return self;
}

-(NSMutableArray*)loadAnimationWithNumberOfFrames:(int) numberOfFrames withFrameName:(NSString*)frame{
    NSMutableArray *anim = [[NSMutableArray alloc]init];
    for (int i = 0 ; i < numberOfFrames ; i++)
    {
        NSString *frameName = [[NSString alloc]init];
        frameName = [NSString stringWithFormat:@"%@%d.png",frame,i];
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
        [anim addObject:frame];
    }
    return anim;
}

-(CCAnimation *)loadAnimationFromCacheByName:(NSString *)_name withCount:(int)_frameCount delay:(float)_delay {
    CCAnimation *result = [[CCAnimationCache sharedAnimationCache] animationByName:_name];
    
    if(result == nil) {
        NSMutableArray *listFrameAnimation = [self loadAnimationWithNumberOfFrames:_frameCount withFrameName:_name];
        result = [CCAnimation animationWithFrames:listFrameAnimation delay:_delay];
        
        [[CCAnimationCache sharedAnimationCache] addAnimation:result name:_name];
        
        [listFrameAnimation removeAllObjects];
        [listFrameAnimation release];
    }
    
    return result;
}

-(void)loadBulletComponent {
    //Implement in sub class
}

-(void)moveAnimationWithFrameName:(NSString *)_frameName frameCount:(int)_frameCount {
    NSMutableArray *arrayFrame = [self loadAnimationWithNumberOfFrames:_frameCount withFrameName:_frameName];
    CCAnimation *animation = [CCAnimation animationWithFrames:arrayFrame delay:0.075];
    
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
}

-(void)fireTarget:(CGPoint)_targetPoint {
    targetPoint = _targetPoint;
}

-(void)hitTargetCallback {
    //[self.spriteBody stopAllActions];
    
    //Call tower is hit target
    [(TowerObject*)tower hitTargetOfBullet:self];
}

-(void)moveToTarget {
    
}

-(void)update:(ccTime)_timeUpdate {

}

-(void)destroySelf {
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    [spriteBody removeFromParentAndCleanup:YES];
    [handler removeGameObject:self];
}

-(void)dealloc {
    [super dealloc];
}

@end
